package command;

/**
 * 紧急停止命令
 */
public class EmergencyStopCommand implements Command {
    private final AgriculturalDevice device;
    private boolean wasRunning = false; // 记录执行前的状态

    public EmergencyStopCommand(AgriculturalDevice device) {
        this.device = device;
    }

    @Override
    public void execute() {
        wasRunning = device.isRunning();
        System.out.println("紧急停止设备: " + device.getDeviceId());
        device.emergencyStop();
    }

    @Override
    public void undo() {
        if (wasRunning) { // 如果执行前设备在运行，则撤销时启动设备
            System.out.println("撤销紧急停止: " + device.getDeviceId());
            device.start();
        }
    }
}
